In [5]:
from IPython.display import display, Image, HTML
from talktools import website, nbviewer
The Jupyter Notebook is a web-based application that enables users to create documents that combine live code wth narrative next, equations, images, visualizations and HTML/JavaScript widgets.
This notebook gives an overview of the Jupyter Notebook and the standard IPython Kernel for running Python code.
First and foremost, Jupyter is an interactive environment for writing and running code. We provide features to make this as pleasant as possible.
In [2]:
2+2
Out[2]:
In [3]:
import math
In [4]:
math.atan?
Inline plotting:
In [5]:
%pylab inline
In [6]:
plot(rand(50))
Out[6]:
Seamless access to the system shell:
In [8]:
!ls -al
In addition to code cells, the Notebook offers Markdown cells, which enable the user to create narrative text with embedded LaTeX equations. Here is a cell that includes Maxwell's equations:
\begin{aligned} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}Markdown cells enable users to create complex narratives that tell stories using code and data.
We are calling this literate computing as it is similar to Knuth's literate programming, but involves live code and data.
Programming langauges, including Python, allow the writing of textual output to stdout and stderr. Jupyter and IPython extend this idea and allows objects to declare rich output representations:
In IPython, the display function is like print for these rich representations:
In [9]:
from IPython.display import display
The Image object has a JPEG/PNG representation that is rendered by the Notebook:
In [10]:
from IPython.display import Image
In [11]:
i = Image("images/jupyter_logo.png")
This representation is displayed if the object is returned from an expression:
In [12]:
print(i)
In [13]:
i
Out[13]:
Or you can manually display the object using display:
In [14]:
display(i)
The HTML object has an HTML representation:
In [15]:
from IPython.display import HTML
In [16]:
s = """<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>"""
In [17]:
h = HTML(s)
In [18]:
display(h)
The Javascript object has a "representation" that runs JavaScript code in the context of the Notebook.
In [ ]:
from IPython.display import Javascript
In [ ]:
display(Javascript("alert('hi');"))
This display architecture also understands objects that have a LaTeX representation. This is best illustrated by SymPy, which is a symbolic mathematics package for Python.
In [1]:
from __future__ import division
from sympy import *
x, y, z = symbols("x y z")
init_printing(use_latex='mathjax')
When a symbolic expression is passed to display or returned from an expression, the LaTeX representation is computed and displayed in the Notebook:
In [2]:
Rational(3,2)*pi + exp(I*x) / (x**2 + y)
Out[2]:
In [3]:
(1/cos(x)).series(x, 0, 12)
Out[3]:
nbviewer is a website for sharing Jupyter Notebooks on the web. It uses nbconvert to create a static HTML rendering of any notebook on the internet. This makes it easy to share notebooks with anyone in the world, without their having to install, or even know anything about, Jupyter or IPython.
In [6]:
website('https://nbviewer.jupyter.org')
Out[6]: